
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/


#include "CMaterial.h"

#include "CProcTextureChecker.h"
#include "CSimpleScene.h"

//constructor
CMaterial::CMaterial(): /*m_proceduralTexture(0),*/ m_bumpMap(0), m_diffuseTexture(0){}

//destructor
CMaterial::~CMaterial()
{
	/*if(m_proceduralTexture != 0)
	{
		delete m_proceduralTexture;
		m_proceduralTexture = 0;
	}*/

	if(m_bumpMap != 0)
	{
		delete m_bumpMap;
		m_bumpMap = 0;
	}
}

//set the ambient component using a color
void CMaterial::setAmbient(const CColor& ambient)
{
	_ambient.setColor(ambient);
	_ambient.setComponentSource(CS_COLOR);
}

//set the ambient component using a procedural texture
void CMaterial::setAmbient(IProcTexture* const procTexture)
{
	_ambient.setProceduralTexture(procTexture);
	_ambient.setComponentSource(CS_PROC_TEXTURE);
}

//set the ambient component using a texture from a file
void CMaterial::setAmbient(CTexture* const fromFileTexture)
{
	_ambient.setFileTexture(fromFileTexture);
	_ambient.setComponentSource(CS_FILE_TEXTURE);
}

//set the diffuse component using a color
void CMaterial::setDiffuse(const CColor& diffuse)
{
	_diffuse.setColor(diffuse);
	_diffuse.setComponentSource(CS_COLOR);
}

//set the diffuse component using a procedural texture
void CMaterial::setDiffuse(IProcTexture* const procTexture)
{
	_diffuse.setProceduralTexture(procTexture);
	_diffuse.setComponentSource(CS_PROC_TEXTURE);
}

//set the diffuse component using a file texture
void CMaterial::setDiffuse(CTexture* const fromFileTexture)
{
	_diffuse.setFileTexture(fromFileTexture);
	_diffuse.setComponentSource(CS_FILE_TEXTURE);
}

//set the specular component using a color
void CMaterial::setSpecular(const CColor& specular)
{
	_specular.setColor(specular);
	_specular.setComponentSource(CS_COLOR);
}

//set the specular component using a procedural texture
void CMaterial::setSpecular(IProcTexture* const procTexture)
{
	_specular.setProceduralTexture(procTexture);
	_specular.setComponentSource(CS_PROC_TEXTURE);
}

//set the specular component using a file texture
void CMaterial::setSpecular(CTexture* const fromFileTexture)
{
	_specular.setFileTexture(fromFileTexture);
	_specular.setComponentSource(CS_FILE_TEXTURE);
}

void CMaterial::setBumpMap(IProcTexture* bump)
{
	m_bumpMap = bump;
}

//Return the ambient color of the component on the point v in the primitive p.
CColor CMaterial::getAmbient(const CVector3f& v, const IPrimitive& p)const
{
	return _ambient.getColor(v, p);
}

//Return the specular color of the component on the point v in the primitive p.
CColor CMaterial::getSpecular(const CVector3f& v, const IPrimitive& p)const
{
	return _specular.getColor(v, p);
}

//Return the diffuse color of the component on the point v in the primitive p.
CColor CMaterial::getDiffuse(const CVector3f& v, const IPrimitive& p)const
{
	return _diffuse.getColor(v, p);
}

CVector3f CMaterial::getBumpNormal(const CVector3f& v)const
{
	if(m_bumpMap != 0)
		return m_bumpMap->getColor(v);


	return CColor();
}

bool CMaterial::useBumpMapping()const
{
	if(m_bumpMap == 0)
		return false;

	return true;
}